package handler

import (
	errno "entrytask/pkg/error"
	"github.com/gin-gonic/gin"
	"net/http"
)

// Response web响应结构体
type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

// SendResponse 正常成功响应
func SendResponse(
	c *gin.Context,
	err error,
	data interface{},
) {
	code, message := errno.DecodeErr(err)
	// always return http.StatusOK
	c.JSON(http.StatusOK, Response{
		Code:    code,
		Message: message,
		Data:    data,
	})
}

// SendResponseWithCookie 设置cookie响应
func SendResponseWithCookie(c *gin.Context, token string, err error, data interface{}) {

	code, message := errno.DecodeErr(err)

	c.SetCookie("token", token, 3600, "/", "localhost", false, true)

	// always return http.StatusOK
	c.JSON(http.StatusOK, Response{
		// set cookie
		Code:    code,
		Message: message,
		Data:    data,
	})

}

// SendResponseRedirect 重定向响应
func SendResponseRedirect(c *gin.Context) {
	c.Redirect(http.StatusMovedPermanently, "../static/index.html")
}
